
import java.util.*;

public class MapSetTest {

    public int singleNumber2(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int x:nums) {
            if(!map.containsKey(x)) {
                map.put(x, 1);
            }else{
                int val = map.get(x);
                map.put(x,val+1);
            }
        }
        for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
            if(entry.getValue() == 1){
                return entry.getKey();
            }
        }
        return -1;
    }

    public int singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int x:nums){
            if(!set.contains(x)){
                set.add(x);
            }else{
                set.remove(x);
            }
        }
        for(int x:nums){ //找到那一个数！！
            if(set.contains(x)){
                return x;
            }
        }
        return -1;
    }


    //去重
    public Set<Integer> distinct(int[] arr){
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            set.add(arr[i]);
        }
        return set;
    }

    //找到第一个重复的值
    public void ontNum(int[] arr){
        Set<Integer> set = new HashSet<>();
        for (int x:arr) {
            if(!set.contains(x)){ //如果set不包含x，放进去
                set.add(x);
            }else { //包含x，打印，找到第一个重复的值
                System.out.println(x);
                return;
            }
        }

    }
    public void func(int[] arr){
        Map<Integer,Integer> map = new HashMap<>();
        for (int x : arr) {
            if(map.get(x) == null){// 不包含
               map.put(x,1);
            }else {
               int val =map.get(x); //获取原来的 value值
               map.put(x,val+1);
            }
        }
        for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
            if(entry.getValue() > 1){
                System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
            }
        }
    }

    public static void main(String[] args) {
        Set<String> set1 = new TreeSet<>();
        //1. 添加元素
        set1.add("abc");
        set1.add("htht");
        set1.add("abc");  //重复的元素不会被添加
        System.out.println(set1);  //[abc, htht]
        //2. 判断“abc”是否存在集合中
        System.out.println(
                set1.contains("abc") ? "存在" : "不存在"
        );
        //3. 返回迭代器
        Iterator<String> iterator = set1.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        /*
           [abc, htht]
            存在
            abc htht
         */

    }
    public static void main6(String[] args) {
        Student student1 = new Student("1");
        Student student2 = new Student("2");
        Student student3 = new Student("3");
        Student student4 = new Student("4");
        Student student5 = new Student("5");
        Student student6= new Student("6");
        Student student7 = new Student("7");
        Student student8 = new Student("8");
        Student student9 = new Student("9");
        HashBuck2<Student,String> hashBuck2 = new HashBuck2<>();
        hashBuck2.put(student1,"lijiajia");
        hashBuck2.put(student2,"lijiajia");
        hashBuck2.put(student3,"wanghang");
        hashBuck2.put(student4,"join");
        hashBuck2.put(student5,"june");
        hashBuck2.put(student6,"sss");
        hashBuck2.put(student7,"dddd");
        hashBuck2.put(student8,"ccc");
        hashBuck2.put(student9,"saaa");
    }
    public static void main5(String[] args) {
        Student student = new Student("1234");
        System.out.println(student.hashCode());

        Student student2 = new Student("1234");
        System.out.println(student2.hashCode());
    }
    public static void main4(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,11);
        hashBuck.put(2,22);
        hashBuck.put(3,33);
        hashBuck.put(4,44);
        hashBuck.put(9,99);
        hashBuck.put(14,144);
        hashBuck.put(6,66);
        hashBuck.put(7,77);
        hashBuck.put(8,88);
        Integer val = hashBuck.get(4);
        System.out.println(val);


    }
    public static void main3(String[] args) {
        Set<String> set1 = new TreeSet<>();
        //1. 添加元素
        set1.add("abc");
        set1.add("htht");
        set1.add("abc");  //重复的元素不会被添加
        System.out.println(set1);  //[abc, htht]
        //2. 判断“abc”是否存在集合中
        System.out.println(
                set1.contains("abc") ? "存在" : "不存在"
        );
        //3. 返回迭代器
        Iterator<String> iterator = set1.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
        /*
           [abc, htht]
            存在
            abc htht
         */


    }
    public static void main2(String[] args) {
        Map<String,Integer> treeMap = new TreeMap<>();
        //1. 设置 key对应的value
        treeMap.put("hello",3);
        treeMap.put("abc",4);
        treeMap.put("the",8);
        treeMap.put("the",18);   //如若有两个相同的，就会更新value值
        //treeMap.put(null,18);    //在TreeMap中插入键值对时，key不能为空，否则会报空指针异常。value可为空
                                 //在HashMap中key和value都可以为空
        System.out.println(treeMap);
        //2. 返回key对应的value
        Integer val = treeMap.get("hello");
        System.out.println(val);
        //3. 返回key对应的value，key不存在，就返回默认值
        Integer val2 = treeMap.getOrDefault("hello2",404);
        System.out.println(val2);
        //4. 返回所有key的不重复集合
        Set<String> keySet = treeMap.keySet();
        System.out.println(keySet);
        System.out.println("-----------");
        //5. 返回所有key-value映射关系
        Set<Map.Entry<String,Integer>> set = treeMap.entrySet();
        //5.1  遍历
        for (Map.Entry<String,Integer> entry:set) {
            System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
        }
        /*运行结果
            {abc=4, hello=3, the=18}
            3
            404
            [abc, hello, the]
            -----------
            key:abc value:4
            key:hello value:3
            key:the value:18
        */

    }
    public static void main1(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(45);
        binarySearchTree.insert(4);
        binarySearchTree.insert(95);
        binarySearchTree.insert(10);
        binarySearchTree.insert(42);
        binarySearchTree.insert(0);
        binarySearchTree.inorder(binarySearchTree.root);
        System.out.println();
        System.out.println("查找到："+binarySearchTree.find(4).val);
    }
}
